import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:xiongan_app_yy/common/user_singleton.dart';
import 'package:xiongan_app_yy/net/internet_api.dart';
import 'package:xiongan_app_yy/pages/common/LoadBuilder.dart';
import 'package:xiongan_app_yy/provider/automounting_provider.dart';
import 'package:xiongan_app_yy/provider/autopicking_provider.dart';
import 'package:xiongan_app_yy/utils/toast_utils.dart';
import 'package:xiongan_app_yy/widgets/comm/left_icon_widget.dart';

//自动拣选——详情列表
class ExecutiveSamplingList extends StatefulWidget {
  String id;
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _ExecutiveSamplingList();
  }

  ExecutiveSamplingList(this.id);
}

//ListWidget
class _ExecutiveSamplingList extends State<ExecutiveSamplingList> {
  List<dynamic> _data = List();
  bool load=true;


  @override
  void initState() {
    getData();
  }

  @override
  Widget build(BuildContext context) {

    return WillPopScope(onWillPop: ()async {
      Navigator.pop(context,"back");
      return false;
       },child:  Scaffold(
        appBar: AppBar(
          leading: LeftIconWidget(icon: Icons.chevron_left, isize: 36),
          backgroundColor: Color(0XFF00A5F1),
          title: Text(
            "执行抽样详情列表",
            style: TextStyle(fontSize: 18),
          ),
          elevation: 0,
          centerTitle: true,
        ),
        body: LoadBuilder(
          visibility: load,
          builder: RefreshIndicator(child: ListView.separated(
              itemBuilder: (BuildContext context, int index) {
                return TextCartItem(
                  data: _data,
                  index: index,
                  onTapFuns: (int index) {
                    Navigator.of(context)
                        .pushNamed("/executivesampling_detail",arguments: _data[index]).then((data){
                      if(data=="ok"){
                        setState(() {
                          load=true;
                        });
                        getData();
                      }
                    });
                  },
                );
              },
              separatorBuilder: (BuildContext context, int index) {
                return Divider(color: Colors.black26);
              },
              itemCount: _data.length),onRefresh: _refresh,)
        )));

  }

  Future <void> _refresh()  async{
    await Future.delayed(Duration(seconds: 1),(){
      return null;
    });
    setState(() {
      load=true;
    });
    getData();
  }

  void getData() {
    InternetAPI.getInstance().get(
      "WTesting/DescSpotcheckTaskDetail/GetAllDescSpotcheckTaskDetail",
          (data) {
        print(data.toString());
        setState(() {
          load=false;
          _data=data;
        });
      },
      params: {
        "operaterUserName": DataSingleton().Data.username,
        "operaterUserId": DataSingleton().Data.id,
        "tenantId": DataSingleton().Data.tenantId,
        "spotchecktaskid": widget.id,
        "pageno": 1,
        "pageSize": -1,
      },
    );
  }

}

class _Text extends Text {
  TextStyle style;
  _Text(String data,
      {this.style = const TextStyle(color: Color(0xff646464), fontSize: 14)})
      : super(data, style: style);
}

class TextCartItem extends StatelessWidget {
  final List<dynamic> data;
  final int index;
   Function(int index) onTapFuns;

  TextCartItem(
      {Key key, @required this.data, @required this.index, this.onTapFuns})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    Color textColor =
        data[index]['ispickup'] == true ? Color(0xFF2FC480) : Color(0xFF00A5F1);
    return GestureDetector(
      child: Container(
          padding: EdgeInsets.all(10.0),
          decoration: BoxDecoration(
              //解决GestureDetector Widget事件触发面问题
              border: Border.all(color: Colors.transparent, width: 1)),
          child: Column(
            children: <Widget>[
              Align(
                alignment: Alignment.bottomLeft,
                child: _Text("物资名称:${data[index]['materialname']??"暂无"}"),
              ),
              Container(
                padding: EdgeInsets.only(top: 5, bottom: 5),
                child: Align(
                  alignment: Alignment.bottomLeft,
                  child: _Text("下架库区:${data[index]['areaname']}"),
                ),
              ),
              Container(
                padding: EdgeInsets.only(top: 5, bottom: 5),
                child: Align(
                  alignment: Alignment.bottomLeft,
                  child: _Text("下架货架:${data[index]['shelfname']??"暂无"}"),
                ),
              ),
              Container(
                padding: EdgeInsets.only(top: 5, bottom: 5),
                child: Align(
                  alignment: Alignment.bottomLeft,
                  child: _Text("下架货位:${data[index]['locationname']}"),
                ),
              ),
              Container(
                padding: EdgeInsets.only(top: 5),
                child: Align(
                  alignment: Alignment.bottomLeft,
                  child: RichText(
                    text: TextSpan(
                      text: '抽样数量:',
                      style:
                          TextStyle(fontSize: 14.0, color: Color(0xff646464)),
                      children: <TextSpan>[
                        TextSpan(
                          text: data[index]['quantity'].toString(),
                          style: TextStyle(fontSize: 14.0, color: textColor),
                        ),
                      ],
                    ),
                  ),
                ),
              ),
            ],
          )),
      onTap: () {
        if (onTapFuns != null) {
          onTapFuns(index);
        }
      },
    );
  }
}
